<template>
	<div>
		<el-card class="box-card">
			<div class="Serach_b">
				<el-button type="primary" @click="AddClick()">添加</el-button>
				<el-input type="input" autosize placeholder="请输入分类名称" v-model="fy.cateName"
					@keyup.enter.native="seachEnter" style="width: 300px;margin-left: 20px;">
				</el-input>
				<el-button type="primary" icon="el-icon-search" @click="SearchData()">搜索</el-button>
			</div>
			<el-table :data="ShopCateList" border>
				<el-table-column prop="id" label="分类主健" />
				<el-table-column prop="cateName" label="分类名称" />
				<el-table-column prop="pic" label="图标">
					<template slot-scope="scope">
						<el-image :src="scope.row.pic" :preview-src-list="ImageList">
						</el-image>
					</template>
				</el-table-column>
				<el-table-column label="是否启用">
					<template slot-scope="scope">
						<span v-if="scope.row.isShow == 0">禁用</span>
						<span v-else>启用</span>
					</template>
				</el-table-column>
				<el-table-column prop="createBy" label="创建人" />
				<el-table-column prop="createTime" label="创建时间" />
				<el-table-column prop="updateBy" label="修改人" />
				<el-table-column prop="updateTime" label="修改时间" />
				<el-table-column label="操作" width="150px">
					<template slot-scope="scope">
						<div class="button">
							<el-button type="primary" size="mini" @click="UpdateClick(scope.row)">修改</el-button>
							<el-button type="danger" size="mini" @click="DelClick(scope.row)">删除</el-button>
						</div>
					</template>
				</el-table-column>
			</el-table>
			<el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
				<el-form label-position="left" label-width="80px" :model="ObjectList">
					<el-form-item label="分类名称">
						<el-input placeholder="请输入分类名称" v-model="ObjectList.cateName"></el-input>
					</el-form-item>

					<el-form-item label="图标">
						<!-- <el-input
              placeholder="请输入图标"
              v-model="ObjectList.pic"
            ></el-input> -->
						<el-upload class="avatar-uploader" action="" :http-request="handleUoload"
							:show-file-list="false" :before-upload="beforeAvatarUpload">
							<img v-if="image" :src="ipconfig.image+image" class="avatar" />
							<i v-else class="el-icon-plus avatar-uploader-icon"></i>
						</el-upload>
					</el-form-item>
					<el-form-item label="是否启用" v-if="this.ObjectList.id">
						<el-select v-model="ObjectList.isShow" placeholder="请选择">
							<el-option v-for="item in ShowList" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					</el-form-item>
				</el-form>
				<span slot="footer" class="dialog-footer">
					<el-button @click="dialogVisible = false">取 消</el-button>
					<el-button type="primary" @click="ClickUser()">确 定</el-button>
				</span>
			</el-dialog>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 15]"
				:page-size="fy.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
			</el-pagination>
		</el-card>
	</div>
</template>

<script>
	import * as ShopCateApi from "@/api/Search";
	import * as syShopCateApi from "@/api/Add";
	import {
		timestampToTime
	} from "@/utils/date";
	import ipconfig from "@/utils/ipconfig"
	export default {
		data() {
			return {
				ipconfig: ipconfig,
				total: 0,
				fy: {
					size: 10,
					current: 1,
				},
				timestampToTime,
				ShopCateList: [],
				ImageList: [],
				title: "",
				image: "",
				dialogVisible: false,
				ObjectList: {},
				ShowList: [{
						label: "禁用",
						value: 0,
					},
					{
						label: "启用",
						value: 1,
					},
				],
			};
		},
		methods: {
			// 上传图片
			handleUoload(file) {
				let formData = new FormData();
				formData.append("file", file.file);
				syShopCateApi.postUpload(formData).then((res) => {
					if (res.code === 200) {
						this.image = res.data;
					}
				});
			},
			beforeAvatarUpload(file) {
				if (file.size > 5242880) {
					this.$message.error("图片大小在5MB以内！");
					return false;
				}
			},
			AddClick() {
				this.title = "新增商品分类信息";
				this.dialogVisible = true;
				this.ObjectList = {};
				this.image = ""
			},
			ClickUser() {
				if (this.ObjectList.id) {
					syShopCateApi.UpdateStoreCategory({
						...this.ObjectList,
						pic: this.image
					}).then((res) => {
						if (res.data == true) {
							this.dialogVisible = false;
							this.SearchData();
							this.$notify({
								title: "成功",
								message: "修改成功",
								type: "success",
							});
						} else {
							this.dialogVisible = false;
							this.SearchData();
							this.$notify({
								title: "失败",
								message: "修改失败",
								type: "error",
							});
						}
					});
				} else {
					syShopCateApi.AddStoreCategory({
						...this.ObjectList,
						pic: this.image
					}).then((res) => {
						if (res.data == true) {
							this.dialogVisible = false;
							this.SearchData();
							this.$notify({
								title: "成功",
								message: "新增成功",
								type: "success",
							});
						} else {
							this.dialogVisible = false;
							this.SearchData();
							this.$notify({
								title: "失败",
								message: "新增失败",
								type: "error",
							});
						}
					});
				}
			},
			UpdateClick(row) {
				syShopCateApi.UpdateStoreCategoryID({
					id: row.id
				}).then((res) => {
					this.ObjectList = res.data;
					this.image = res.data.pic
					this.dialogVisible = true;
				});
				this.title = "修改商品分类信息";
			},
			DelClick(row) {
				this.$confirm("确认删除？")
					.then((_) => {
						syShopCateApi.DelStoreCategory({
							idList: row.id
						}).then((res) => {
							if (res.data == true) {
								this.dialogVisible = false;
								this.SearchData();
								this.$notify({
									title: "成功",
									message: "删除成功",
									type: "success",
								});
							} else {
								this.dialogVisible = false;
								this.SearchData();
								this.$notify({
									title: "失败",
									message: "删除失败",
									type: "error",
								});
							}
						});
						done();
					})
					.catch((_) => {});
			},
			SearchData() {
				ShopCateApi.InsertShopCate(this.fy).then((res) => {
					this.total = res.data.total;
					this.ShopCateList = res.data.records;
					this.ShopCateList.forEach((item) => {
						this.ImageList.push(this.ipconfig.image + item.pic);
					});
				});
			},
			handleSizeChange(value) {
				this.fy.size = value;
				this.SearchData();
			},
			handleCurrentChange(value) {
				this.fy.current = value;
				this.SearchData();
			},
			handleClose(done) {
				this.$confirm("确认关闭？")
					.then((_) => {
						done();
					})
					.catch((_) => {});
			},
		},
		created() {
			this.SearchData();
		},
	};
</script>

<style lang="scss" scoped>
	.box-card {
		width: 95%;
		margin: auto;
	}

	.el-table {
		font-size: 10px;
	}

	.el-pagination {
		margin-top: 10px;
	}

	.Serach_b {
		display: flex;
		align-items: center;
		margin-bottom: 20px;
	}

	.Serach_b .el-button {
		margin-left: 20px;
	}

	.el-textarea {
		margin-left: 20px;
		width: 20%;
	}

	::v-deep.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409eff;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
